package com.application.DataProvider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class DataAccess {
	private static DataProvider _provider;
	public static int insert(String table, ArrayList<String> attributes, ArrayList<String> values, SQLiteDatabase db)
	{
		int result = 0;
		try {
			ContentValues val = new ContentValues();
			int n = attributes.size();
			for(int i = 0; i<n; i++)
			{
				val.put(attributes.get(i), values.get(i));
			}
			result = get_provider().execInsertCommand(db, table, val);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	public static int delete(String table, SQLiteDatabase db)
	{
		int result = 0;

		try {
			String sql = "delete from " + table;
			result= _provider.execDeleteCommand(db, table, null, null);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	public static int deleteRecord(String table, int id, SQLiteDatabase db)
	{
		int result = 0;

		try {
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};	
			result= _provider.execDeleteCommand(db, "Subject", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	public static int update(String table, int id, ArrayList<String> attributes, ArrayList<String> values, SQLiteDatabase db)
	{
		int result=0;

		try{
			ContentValues val = new ContentValues();
			int n = attributes.size();
			for(int i = 0; i<n; i++)
			{
				val.put(attributes.get(i), values.get(i));
			}

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};
			result= _provider.execUpdateCommand(db, "Subject", val, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static void set_provider(DataProvider _provider) {
		DataAccess._provider = _provider;
	}

	public static DataProvider get_provider() {
		return _provider;
	}
}
